package com.heima.kafka.sample;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/**
 * 消息生产者
 */
public class ProducerFastStart {

    public static void main(String[] args) {
        //kafka的配置信息
        Properties properties = new Properties();
        //kafka的连接地址
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.200.130:9092");
        //发送失败，失败的重试次数
        properties.put(ProducerConfig.RETRIES_CONFIG,5);
        //消息key的序列化器
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
        //消息value的序列化器
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");

        //生产者对象
        KafkaProducer<String,String> producer = new KafkaProducer<String, String>(properties);

        //封装发送的消息
        ProducerRecord<String,String> record = new ProducerRecord<String, String>("itheima-topic","100001","hello kafka");

        //发送消息
        producer.send(record);

        //关闭消息通道，必须关闭，否则消息发送不成功
        producer.close();
    }

    //以下为自己练手写的
//public static void main(String[] args) {
//    //kafka的配置信息
//    Properties properties = new Properties();
//    //kafka的连接地址
//    properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.200.130:9092");
//    //发送失败，失败的重试次数
//    properties.put(ProducerConfig.RETRIES_CONFIG,10);
//    //消息key的序列化器
//    properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,"序列化器的全限定类名");
//    //消息value的序列化器
//    properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"序列化器的全限定类名");
//
//    //生产者对象
//    KafkaProducer<String, String> producer = new KafkaProducer<String, String>(properties);
//    //封装发送的消息
//    ProducerRecord<String, String> record = new ProducerRecord<>("topic", "你好啊 小瘪三");
//    //发送消息
//   producer.send(record);
//
//    //关闭消息通道，必须关闭，否则消息发送不成功
//    producer.close();
//}
}